<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/17
 * Time: 15:20
 */

namespace app\index\controller;



use app\common\model\MemberAddress;
use app\common\model\Region;
use app\common\utils\JsonService;
use think\response\Json;

class Member extends MemberBase
{

    public function index(){

        return $this->fetch();
    }


    /*
       * 用户地址列表
       */
    public function address_list(){
        $address_lists = get_user_address_list($this->member_id);
        $region_list = get_region_list();
        $this->assign('region_list',$region_list);
        $this->assign('lists',$address_lists);
        $this->assign('active','address_list');

        return $this->fetch();
    }
    /*
     * 添加地址
     */
    public function add_address(){
        header("Content-type:text/html;charset=utf-8");
        if($this->request->isPost()){
            $member_id = $this->member_id;
            $data = MemberAddress::add_address($member_id,0,input('post.'));
            if($data['status'] != 1)
                exit('<script>alert("'.$data['msg'].'");history.go(-1);</script>');
            $call_back = $_REQUEST['call_back'];
            echo "<script>parent.{$call_back}('success');</script>";
            exit(); // 成功 回调closeWindow方法 并返回新增的id
        }
        $p = Region::where(array('parent_id'=>0,'level'=> 1))->select()->toArray();
        $address = MemberAddress::where(['address_id'=>0,'member_id'=> $this->member_id])->find();

        $this->assign('province',$p);
        $this->assign('address',$address);
        return $this->fetch('edit_address');

    }

    /*
     * 地址编辑
     */
    public function edit_address(){
        header("Content-type:text/html;charset=utf-8");
        $id = input('get.id/d');
        $address = MemberAddress::where(array('address_id'=>$id,'member_id'=> $this->member_id))->find();
        if($this->request->isPost()){
            $data = MemberAddress::add_address($this->member_id,$id,input('post.'));
            if($data['status'] != 1)
                exit('<script>alert("'.$data['msg'].'");history.go(-1);</script>');

            $call_back = $_REQUEST['call_back'];
            echo "<script>parent.{$call_back}('success');</script>";
            exit(); // 成功 回调closeWindow方法 并返回新增的id
        }
        //获取省份
        $p = Region::where(array('parent_id'=>0,'level'=> 1))->select();
        $c = Region::where(array('parent_id'=>$address['province'],'level'=> 2))->select();
        $d = Region::where(array('parent_id'=>$address['city'],'level'=> 3))->select();
        if($address['twon']){
            $e = Region::where(array('parent_id'=>$address['district'],'level'=>4))->select();
            $this->assign('twon',$e);
        }

        $this->assign('province',$p);
        $this->assign('city',$c);
        $this->assign('district',$d);
        $this->assign('address',$address);
        return $this->fetch();
    }

    /*
     * 设置默认收货地址
     */
    public function set_default(){
        $id = I('get.id/d');
        MemberAddress::where(array('member_id'=>$this->member_id))->save(array('is_default'=>0));
        $row = MemberAddress::where(array('member_id'=>$this->member_id,'address_id'=>$id))->save(array('is_default'=>1));
        if(!$row)
            $this->error('操作失败');
        $this->success("操作成功");
    }

    /*
     * 地址删除
     */
    public function del_address(){
        $id = input('get.id/d');

        $address = MemberAddress::where("address_id", $id)->find();
        $row = MemberAddress::where(array('member_id'=>$this->member_id,'address_id'=>$id))->delete();
        // 如果删除的是默认收货地址 则要把第一个地址设置为默认收货地址
        if($address['is_default'] == 1)
        {
            $address2 = MemberAddress::where("member_id", $this->member_id)->find();
            $address2 && MemberAddress::where("address_id", $address2['address_id'])->save(array('is_default'=>1));
        }

        /*
        if(!$row)
            $this->error('操作失败',url('User/address_list'));
        else
            $this->success("操作成功",U('User/address_list'));
        */
        if(!$row){
            return JsonService::error('删除失败');
        }
        return JsonService::success('删除成功');
    }



}